package common

import (
	"os"
	"spider/thirdparty/log4go.v1"
	"time"
)

const (
	_DEFAULT_CONCURRENT_REQUEST int           = 10 //working gorounties amonut.
	_DEFAULT_DOWNLOADER_NUM     int           = 10 //downloaders num.
	_DEFAULT_DELAY              time.Duration = time.Millisecond * 300
	_DEFAULT_ENABLE_RETRY       bool          = false            //if enable retry
	_DEFAULT_RETRY_TIMES        int           = 3                //retry times
	_DEFAULT_TIMEOUT            time.Duration = 15 * time.Second //timeout
	_DEFAULT_KEEPALIVE          time.Duration = 60 * time.Second //keepalive
	_DEFAULT_DOWNLOAD_DIR       string        = "./download"     //download dir
	_DEFAULT_URL_POOL_SIZE      int           = 10000            //url chan buffer length
	_DEFAULT_STATS_SERVER_ADDR  string        = ":9527"
	_DEFAULT_PROXY_SERVER       string        = "http://127.0.0.1:8080"
)

type SpiderConfig struct {
	CONCURRENT_REQUEST  int
	DOWNLOADER_NUM      int
	DELAY               time.Duration
	ENABLE_KEEPALIVE    bool
	ENABLE_COMPRESSION  bool
	ENABLE_COOKIE       bool
	ENABLE_RETRY        bool
	RETRY_TIMES         int
	TIMEOUT             time.Duration
	KEEPALIVE           time.Duration
	DOWNLOAD_DIR        string
	URL_POOL_SIZE       int
	STATS_SERVER_ADDR   string
	PROXY_SERVER_SERVER string
	ENABLE_BLOOM_FILTER bool
	ENABLE_STATS_SERVER bool
	ENABLE_PROXY        bool
}

func NewSpiderConfig() *SpiderConfig {
	return &SpiderConfig{
		CONCURRENT_REQUEST:  _DEFAULT_CONCURRENT_REQUEST,
		DOWNLOADER_NUM:      _DEFAULT_DOWNLOADER_NUM,
		DELAY:               _DEFAULT_DELAY,
		ENABLE_RETRY:        _DEFAULT_ENABLE_RETRY,
		RETRY_TIMES:         _DEFAULT_RETRY_TIMES,
		TIMEOUT:             _DEFAULT_TIMEOUT,
		DOWNLOAD_DIR:        _DEFAULT_DOWNLOAD_DIR,
		URL_POOL_SIZE:       _DEFAULT_URL_POOL_SIZE,
		STATS_SERVER_ADDR:   _DEFAULT_STATS_SERVER_ADDR,
		PROXY_SERVER_SERVER: _DEFAULT_PROXY_SERVER,
	}

}

var logger = log4go.NewDefaultLogger(log4go.INFO)
var DefaultSpiderConfig *SpiderConfig
var DefaultSpiderContext *SpiderContext

func init() {
	DefaultSpiderConfig = NewSpiderConfig()
	DefaultSpiderContext = NewSpdierContext(DefaultSpiderConfig) //init the spider context
	_, err := os.Stat(_DEFAULT_DOWNLOAD_DIR)
	if err != nil {
		os.Mkdir(_DEFAULT_DOWNLOAD_DIR, os.ModeDir)
	}

}
